<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:Commmision:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house_sale:Commmision:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:Commmision:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:Commmision:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CommmisionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="commisionid" hidden/> -->
      <el-table-column label="客户名称" align="center" prop="custname" />
      <el-table-column label="房产名称" align="center" prop="housename" />
      <el-table-column label="业务员" align="center" prop="empid" />
      <!-- <el-table-column label="购买房产编号" align="center" prop="buyid" /> -->
      <el-table-column label="付款金额" align="center" prop="paidmoney" />
      <el-table-column label="付款时间" align="center" prop="paidtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paidtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="userid" />
      <el-table-column label="录入时间" align="center" prop="oprtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oprtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" prop="invalid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button  v-if="scope.row.invalid === '有效'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate2(scope.row)"
          >作废</el-button>
           <el-button  v-if="scope.row.invalid === '作废'"
            size="mini"
            type="text"
            icon="el-icon-delete"
          >已作废</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改佣金管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="custname">
          <el-input v-model="form.custname"  disabled/>
        </el-form-item>
        <el-form-item label="房产名称" prop="housename">
          <el-input v-model="form.housename"  disabled/>
        </el-form-item>
        <el-form-item label="签约总款" prop="totalmoney">
          <el-input v-model="form.totalmoney"  disabled/>
        </el-form-item>
        <el-form-item label="应付佣金" prop="commisionMoney">
          <el-input v-model="form.commisionMoney"  disabled/>
        </el-form-item>
        <el-form-item label="已付佣金" prop="commisionpaid">
          <el-input v-model="form.commisionpaid"  disabled/>
        </el-form-item>
        <el-form-item label="佣金比例" prop="commisionPercent">
          <el-input v-model="form.commisionPercent"  disabled/>
        </el-form-item>
        <el-form-item label="购买房产编号" prop="buyid" hidden>
          <el-input v-model="form.buyid" />
        </el-form-item>
        <el-form-item label="佣金金额" prop="paidmoney">
          <el-input v-model="form.paidmoney"/>
        </el-form-item>
        <el-form-item label="付款时间" prop="paidtime">
          <el-date-picker clearable :style="{width:'100%'}"
            v-model="form.paidtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择付款时间"
             @change="getTime1">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作人" prop="userid" hidden>
          <el-input v-model="form.userid" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="录入时间" prop="oprtime" hidden>
          <el-date-picker clearable
            v-model="form.oprtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择录入时间" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否有效" prop="invalid" hidden>
          <el-input v-model="form.invalid" />
        </el-form-item>
        <el-form-item label="业务员" prop="empid">
          <el-input v-model="form.empid" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommmision, getCommmision, delCommmision, addCommmision, updateCommmision,selectCommmision,beInvalid,updmoney } from "@/api/house_sale/Commmision";

export default {
  name: "Commmision",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 佣金管理表格数据
      CommmisionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        empid: null,
        buyid: null,
        paidmoney: null,
        paidtime: null,
        userid: null,
        oprtime: null,
        invalid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        empid: [
          { required: true, message: "业务员编号不能为空", trigger: "blur" }
        ],
        buyid: [
          { required: true, message: "购买房产编号不能为空", trigger: "blur" }
        ],
        paidmoney: [
          { required: true, message: "已付金额不能为空", trigger: "blur" }
        ],
        paidtime: [
          { required: true, message: "付款时间不能为空", trigger: "blur" }
        ],
        userid: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
        oprtime: [
          { required: true, message: "录入时间不能为空", trigger: "blur" }
        ],
        invalid: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    
    /**自动获取录入时间 */
    getTime1(){
      if(this.form.paidtime!=null){
        this.form.oprtime= this.form.paidtime;
      }
    },
    /** 查询佣金管理列表 */
    getList() {
      this.loading = true;
      const id=this.$route.params.buyid;
      listCommmision(id).then(response => {
        this.CommmisionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commisionid: null,
       
        buyid:  this.$route.params.buyid,
        custname: this.$route.query.custname,
        housename: this.$route.query.housename,
        totalmoney:this.$route.query.signtotalprice,
        commisionPercent :this.$route.query.commisionPercent,
        commisionMoney:this.$route.query.commisionMoney,
        commisionpaid :this.$route.query.commisionpaid,
        paidmoney: null,
        paidtime: null,
        userid: this.$store.state.user.name,
        oprtime: null,
        invalid: "有效",
         empid: this.$route.query.saleperson
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commisionid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加佣金管理";
      // selectCommmision(this.$route.params.buyid).then(response => {
      //   this.form.commisionpaid = response.commisionpaid;
      // });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commisionid = row.commisionid || this.ids
      getCommmision(commisionid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改佣金管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.commisionid != null) {
            updateCommmision(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommmision(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commisionids = row.commisionid || this.ids;
      this.$modal.confirm('是否确认删除佣金管理编号为"' + commisionids + '"的数据项？').then(function() {
        return delCommmision(commisionids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/Commmision/export', {
        ...this.queryParams
      }, `Commmision_${new Date().getTime()}.xlsx`)
    },
     /**作废按钮操作 */
    handleUpdate2(row){
      const commisionid = row.commisionid 
      // const paidmoney = row.paidmoney 
    
      getCommmision(commisionid).then(response => {
        var form2 = response.data;
        // form2.commisionpaid=response.data.commisionpaid-response.data.paidmoney;
        form2.invalid="作废";
        this.$modal.confirm('是否确认作废').then(function() {
         return beInvalid(form2);//去作废
      }).then(() => {
          updmoney(commisionid).then(response => {
              // this.$modal.msgSuccess("修改成功");
              // this.open = false;
              this.getList();
            });
        // this.form.commisionpaid-=paidmoney
        // updmoney(commisionid);
        // this.getList();
        this.$modal.msgSuccess("已作废");
      }).catch(() => {});
      });
    },
  }
};
</script>
